package jcore.jsonrpc.rpcobj;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import jcore.jsonrpc.common.JsonRpcObject;

public class CommunicateStates extends JsonRpcObject implements Serializable {
	
	//所有坐席状态Map
	private static Map<String,CallState> statesMap;
	
	public CommunicateStates() {
		
	}
	
	public CallState getCallState(String key) {
		 Map<String,CallState> map = getStatesMap();
		 if(!map.containsKey(key)){
			 map.put(key, getNewCallState());
			 System.out.println("getCallState---------if");
		 }
		 System.out.println("getCallState---------"+map.get(key).getServerIp());
		 return map.get(key);		 
	}
	
	public void setCallState(String key,Map<String,String> callStateMap) {
		 Map<String,CallState> map = getStatesMap();
		 System.out.println("setCallState---------callStateMap"+callStateMap.get("serverIp"));
		 System.out.println(map.containsKey(key));
		 if(!map.containsKey(key)){
			 map.put(key,MapToCallState(getNewCallState(),callStateMap));
			 System.out.println("setCallState---------if");
		 }else{
			 try{
			 CallState callState = map.get(key);
			 callState = MapToCallState(callState,callStateMap);
			 System.out.println("setCallState---------callState"+callState.getServerIp());
			 map.put(key,callState);
			 }catch(Exception e){
				 e.printStackTrace();
			 }
		 }
	}
	
	private static CallState MapToCallState(CallState callState,Map<String,String> callStateMap) {
		
		callState.setAgentId(callStateMap.get("agentId"));
		callState.setAgentPassword(callStateMap.get("agentPassword"));
		callState.setCalled(callStateMap.get("called"));
		if(callStateMap.get("callId") != null)
			callState.setCallId(Integer.parseInt(callStateMap.get("callId")));
		callState.setFaxCalled(callStateMap.get("faxCalled"));
		callState.setFaxTaskId(callStateMap.get("faxTaskId"));
		callState.setLocalDeviceId(callStateMap.get("localDeviceId"));
		if(callStateMap.get("localDeviceType") != null)
			callState.setLocalDeviceType(Integer.parseInt(callStateMap.get("localDeviceType")));
		callState.setMsgId(callStateMap.get("msgId"));
		callState.setQueue(callStateMap.get("queue"));
		callState.setServerIp(callStateMap.get("serverIp"));
		callState.setSmsCalled(callStateMap.get("smaCalled"));
		
		return callState;
	}
	
	private static Map<String,CallState> getStatesMap() {
		if(statesMap == null){
			statesMap = new HashMap<String,CallState>();
		}
		return statesMap;
	}
	
	private CallState getNewCallState() {
		return new CallState();
	}
	
	private CommunicateStates getCommunicateStatesObj() {
		return this;
	}
	
	public class CallState implements Serializable  {

		private String serverIp;

		private String localDeviceId;
		
		private int localDeviceType;
		
		private String agentId;
		
		private String agentPassword;
		
		private String queue;

		private int callId;
		
		private String called;
		
		private String faxTaskId;
		
		private String faxCalled;
		
		private String msgId;
		
		private String smsCalled;
		
		public CallState() {
			
		}

		private String getServerIp() {
			return serverIp;
		}

		private void setServerIp(String serverIp) {
			this.serverIp = serverIp;
		}

		private String getLocalDeviceId() {
			return localDeviceId;
		}

		private void setLocalDeviceId(String localDeviceId) {
			this.localDeviceId = localDeviceId;
		}

		private int getLocalDeviceType() {
			return localDeviceType;
		}

		private void setLocalDeviceType(int localDeviceType) {
			this.localDeviceType = localDeviceType;
		}

		private String getAgentId() {
			return agentId;
		}

		private void setAgentId(String agentId) {
			this.agentId = agentId;
		}

		private String getAgentPassword() {
			return agentPassword;
		}

		private void setAgentPassword(String agentPassword) {
			this.agentPassword = agentPassword;
		}

		private String getQueue() {
			return queue;
		}

		private void setQueue(String queue) {
			this.queue = queue;
		}

		private int getCallId() {
			return callId;
		}

		private void setCallId(int callId) {
			this.callId = callId;
		}

		private String getCalled() {
			return called;
		}

		private void setCalled(String called) {
			this.called = called;
		}

		private String getFaxTaskId() {
			return faxTaskId;
		}

		private void setFaxTaskId(String faxTaskId) {
			this.faxTaskId = faxTaskId;
		}

		private String getFaxCalled() {
			return faxCalled;
		}

		private void setFaxCalled(String faxCalled) {
			this.faxCalled = faxCalled;
		}

		private String getMsgId() {
			return msgId;
		}

		private void setMsgId(String msgId) {
			this.msgId = msgId;
		}

		private String getSmsCalled() {
			return smsCalled;
		}

		private void setSmsCalled(String smsCalled) {
			this.smsCalled = smsCalled;
		}

	}
}
